package com.neuedu


import org.apache.spark.SparkConf
import org.apache.spark.sql.{SaveMode, SparkSession}


object Change {
  def main(args: Array[String]): Unit = {
    System.setProperty("HADOOP_USER_NAME", "root")


    val sparkConf = new SparkConf()
      .setMaster("local[*]")

    val sparkSession = SparkSession.builder()
      .config(sparkConf)
      .enableHiveSupport()
      .appName("Change")
      .getOrCreate()

    val cPath = "/vehicle/data/c/c_vehicle.1734577582804"
    val dPath = "/vehicle/data/d/d_vehicle.1734576591842"

    val cData = sparkSession.read.option("header", "true").csv(cPath)
    val cDataRenamed = cData
      .withColumnRenamed("时间", "times")
      .withColumnRenamed("新能源汽车产量", "nev_production")
      .withColumnRenamed("新能源汽车销量", "nev_sales")
      .withColumnRenamed("纯电动汽车产量", "bev_production")
      .withColumnRenamed("纯电动汽车销量", "bev_sales")
      .withColumnRenamed("插电式混合动力汽车产量", "phev_production")
      .withColumnRenamed("插电式混合动力汽车销量", "phev_sales")

    val dData = sparkSession.read.option("header", "true").csv(dPath)
    val dDataRenamed = dData
      .withColumnRenamed("时间", "times")
      .withColumnRenamed("公共类充电桩数量_重庆", "chongqing")
      .withColumnRenamed("公共类充电桩数量_北京", "beijing")
      .withColumnRenamed("公共类充电桩数量_广东", "guangdong")
      .withColumnRenamed("公共类充电桩数量_上海", "shanghai")
      .withColumnRenamed("公共类充电桩数量_浙江", "zhejiang")
      .withColumnRenamed("公共类充电桩数量_安徽", "anhui")
      .withColumnRenamed("公共类充电桩数量_天津", "tianjin")
      .withColumnRenamed("公共类充电桩数量_四川", "sichuan")
      .withColumnRenamed("公共类充电桩数量_山东", "shandong")
      .withColumnRenamed("公共类充电桩数量_江苏", "jiangsu")
      .withColumnRenamed("公共类充电桩数量_直流桩_全国", "dc_china")
      .withColumnRenamed("公共类充电桩数量_交流桩_全国", "ac_china")

    cDataRenamed.show()
    dDataRenamed.show()

    cDataRenamed.write.mode(SaveMode.Overwrite).parquet("/vehicle/data/c/c_vehicle_renamed.parquet")

    dDataRenamed.write.mode(SaveMode.Overwrite).parquet("/vehicle/data/d/d_vehicle_renamed.parquet")

    sparkSession.stop()
  }
}

